<template>
  <el-dialog
    v-el-drag-dialog
    title="备注"
    :visible.sync="visible"
    width="600px"
    @closed="reset"
  >
    <el-form ref="dataForm" :model="temp" label-position="right" label-width="80px">
      <el-form-item>
        <label slot="label">备注:</label>
        <el-input v-model="remark" disabled />
      </el-form-item>
      <el-form-item label-width="80px" label="追加备注:" prop="remark">
        <el-input v-model="temp.remark" type="textarea" :rows="3" :maxlength="maxLength" :disabled="readonly" show-word-limit />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">
        取消
      </el-button>
      <el-button :loading="$loadingPlugin.submit" type="primary" @click="success">
        确定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { updateCsmGroup } from '@/api'

  export default {
    name: 'remark',
    data() {
      return {
        visible: false,
        groupCode: '',
        remark: '',
        temp: {
          remark: ''
        },
        maxLength: 220,
        readonly: false
      }
    },
    methods: {
      open(temp) {
        console.log(temp)
        return new Promise(resolve => {
          this.resolve = resolve
          this.visible = true
          if (temp) {
            this.groupCode = temp.groupCode
            this.remark = temp.remark
            console.log(this.remark)
            if (String(this.remark).length < 220) {
              this.readonly = false
              this.maxLength = 220 - String(this.remark).length
              console.log(this.maxLength)
            } else {
              this.maxLength = 0
              this.readonly = true
            }
          }
        })
      },
      success() {
        this.$refs.dataForm.validate(valid => {
          if (!valid) return
          const params = { groupCode: this.groupCode, ...this.temp }
          this.submit(params)
        })
      },
      async submit(params) {
        await updateCsmGroup(params)
        this.resolve()
        this.visible = false
        this.$notify({
          title: '提示',
          message: '追加备注成功',
          type: 'success',
          duration: 2000
        })
      },
      reset() {
        this.temp = {
          remark: ''
        }
        this.$nextTick(() => {
          this.$refs.dataForm.clearValidate()
        })
      }
    }
  }
</script>

<style scoped>

</style>
